import { routerRedux } from 'dva/router';
import { stringify } from 'qs';
import { fakeAccountLogin, getFakeCaptcha ,logout ,accountLogin,phoneLogin,goRegister} from '@/services/api';
import { setAuthority } from '@/utils/authority';
import { getPageQuery } from '@/utils/utils';
import { reloadAuthorized } from '@/utils/Authorized';
import { Modal,message } from 'antd';

export default {
  namespace: 'login',

  state: {
    status: undefined,
  },

  effects: {
    *login({ payload }, { call, put }) {
      let  response ;
      if(payload.type === 'account'){
        delete payload.type;
        response = yield call(accountLogin, payload);
      }else if (payload.type === 'mobile'){
        delete payload.type;
        response = yield call(phoneLogin, payload);
      }
      if(response && response.return_code === 'SUCCESS'){
          setAuthority("after_login");
          window.sessionStorage.setItem("userInfo", JSON.stringify(response.data));
          reloadAuthorized();
          window.location.href = "/" ;
      }
    },

    *getCaptcha({ payload }, { call }) {
     const response =  yield call(getFakeCaptcha, payload);
      if (response && response.return_code === 'SUCCESS') {
        message.success('发送成功');
      }
    },

    *register({ payload }, { call, put }) {
      const response =  yield call(goRegister, payload);
      if (response && response.return_code === 'SUCCESS') {
        message.success('注册成功');
        return true;
      }
    },
    *logout({ payload }, { call, put }) {
      setAuthority('notLogin');
      reloadAuthorized();
      window.location.href = "/user/login" ;
      //yield call(logout);
    },
  },

  reducers: {
    changeLoginStatus(state, { payload }) {
      setAuthority(payload.currentAuthority);
      return {
        ...state,
        status: payload.status,
        type: payload.type,
      };
    },
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
